{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Monty Hall Problem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem Description:\n",
    "The Monty Hall Problem is a very famous problem in Probability Theory. The question goes like:\n",
    "\n",
    "\n",
    "Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, \"Do you want to pick door No. 2?\" Is it to your advantage to switch your choice?\n",
    "\n",
    "By intution it seems that there shouldn't be any benefit of switching the door. But using Bayes' Theorem we can show that by switching the door the contestant has more chances of winning.\n",
    "\n",
    "You can also checkout the wikipedia page: https://en.wikipedia.org/wiki/Monty_Hall_problem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Probabilistic Interpretetion:\n",
    "So have 3 random variables Contestant $C \\in \\{1, 2, 3\\}$, Host $H \\in \\{1, 2, 3\\}$ and prize $P \\in \\{1, 2, 3 \\}$. The prize has been put randomly behind the doors therefore: $P(P=1) = P(P=2) = P(P=3) = \\frac{1}{3}$. Also, the contestant is going to choose the door randomly, therefore: $P(C=1) = P(C=2) = P(C=3) = \\frac{1}{3}$. For this problem we can build a Bayesian Network structure like:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAdcAAAHXCAYAAADuhBDBAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAXEQAAFxEByibzPwAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3X1UVPedx/HPIAio+Byzq2JywKjn4POwjWZ9SI6gJq2ePOBG15ytTX2IaOz2SZNsd3tiztq1x+1JbaqCxCWbRm2jtkm2RlE3krixSRmN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nja2+vjXvvY1bdq0idPDgCE0V8CQu+++WzU1NTd9vKGhQYWFhUGc6Jr9+/ffNFgl6auvvtLdd98dvIGACEe4AoZ07dpVO3bsuGUzderU8K3W2717d23fvl1du3YN4kRAZCNcAYOGDBmi/Pz8mz4eiuGan5+vIUOGBHEaIPIRroBhjz76qH74wx+2+tiJEyd06tSpoM5z8uRJlZSUtPrY8uXL9cgjjwR1HiAaEK6ADVatWqX777+/1cf27NkT1Flutr4HHnhA//qv/xrUWYBoQbgCNoiNjdXWrVvVv3//Gx4LhXAdMGCAtmzZothY3uoO2IG34gA2ev/99zV58uRmV+r27t1bFy5caHaDCbs0NTXpjjvu0Jdffun/XlxcnAoLCzV+/Hjb1w9EKw5bARvdd999+tnPftbsPsNffPGFDh06dNu7NfluSFFVVaW6ujpduXJFnTt3VkJCgpKSkpScnHzbDwbweDzNglWSfvaznxGsgM0IV8BmS5cu1cGDB7Vlyxb/91reCtGyLJWWlsrj8TT7V1lZedPl9ujRQ263u9m/1NTUZoHb8irhv//7v9eSJUsMbh2A1nBaGAiCmpoa3XvvvSouLpYkTZo0SYWFhTp79qw2btyojRs36vPPP7/h52JjY9WjRw8lJiaqc+fOunLlimpra1VZWdnqTSH69++vBQsWaOHCherfv78mTZqk9957T9LVDxX4wx/+wPtZgSAgXIEgKSkpUXp6uqqqqhQTE6MZM2bov/7rv9TU1CRJio+P18iRI+V2u5Weni632620tDTFxcXdsKyGhgYVFxerqKjI33KPHj2q+vp6SVc/MGDGjBl688035fV61b17d/3xj3/k/axAkBCuQBD9y7/8i1544YVm35s0aZIlyj8EAAAL7UlEQVSys7P18MMPKz4+vt3Lrq+v129/+1utX79e77777g3rff7559u9bACBIVyBILh8+bK+//3vKy8vT9LVWyV+85vf1OLFi5t9Bqwpx48f1/r16/XKK6/473e8YMECrVmzRt27dze+PgDNEa6AzQoKCjR//nz/R9EtW7ZML7zwQlBC7vLly/rnf/5nrV27VpKUnJysvLw8TZ061fZ1A9GMcAVsUl9fr6efflobN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k9P99+m8Kmnngq708Pl5eVavHixJOmZZ54hWAEDaK6AAdefHn7ooYf0u9/9TnFxcU6PdVsNDQ16+OGHtXPnTk4HAwbRXAED4uPj9corryghIUE7d+7Ut771LXm9XqfHuiWv16t58+Zp586dSkhIUH5+PsEKGEK4Aoa43W5t27ZNsbGxeu2117Ro0SI1NTU5PVarmpqatGjRIm3evFmxsbHavn07p4MBgwhXwKCvf/3revXVVxUTE6O8vDw98cQTamhocHqsZq5cuaK5c+cqLy9PMTExevXVV/XQQw85PRYQUQhXwLDZs2dr69atiouL09atWzV16lSdOnXK6bEkSadOndK0adP061//WnFxcfr1r3+t2bNnOz0WEHEIV8AGs2bN0ptvvqkuXbpo//79Gj58uP+evU6wLEs5OTkaPny49u/fry5duuitt95SVlaWI/MAkY6rhQEbffrpp3ryySd14MABSVJGRoby8vJ01113BW2GU6dO6dvf/rb27dsnSZo4caI2bdqkwYMHB20GINrQXAEb3XPPPSosLNSLL76oxMRE7d27V8OHD9ePf/xjnT171tZ1nz17Vj/+8Y81fPhw7du3T4mJiXrxxRe1f/9+ghWwGc0VCJKWLbZTp056+OGHlZ2drQceeEAul6vD67AsS//93/+tdevW6Y033vBfrUxbBYKLcAWCyOv1atu2bfrlL3+pd9991//9oUOHas6cOUpPT5fb7dZf/dVftXmZ586dk8fjUVFRkbZs2aITJ074H5s8ebKys7OVlZWlmBhOVAHBQrgCDjl+/LjWr1+v//zP/1R1dXWzxwYMGCC3260xY8aoT58+SkxMVOfOnXXlyhXV1tbq0qVLOnz4sIqKivT55583+9mkpCT9wz/8gxYvXqy0tLRgbhKA/0e4Ag6rqqrSli1b9N5778nj8eiTTz4J6Kpil8ulYcOGye12a9KkSZo9e7aSkpJsnBjA7RCuQIiprq7W4cOH5fF4dOzYMVVVVamurk719fWKj49XQkKCkpKSNGLECH+77datm9NjA7gO4QoAgGFc4QAAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhhGuAAAYRrgCAGAY4QoAgGGEKwAAhv0fej2lyQEiGeIAAAAASUVORK5CYII=",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import Image\n",
    "\n",
    "Image(\"images/monty.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "with the following CPDs:\n",
    "\n",
    "<pre>\n",
    "\n",
    "P(C):\n",
    "+----------+----------+-----------+-----------+\n",
    "|    C     |     0    |     1     |      2    |\n",
    "+----------+----------+-----------+-----------+\n",
    "|          |    0.33  |    0.33   |    0.33   |\n",
    "+----------+----------+-----------+-----------+\n",
    "\n",
    "P(P):\n",
    "+----------+----------+-----------+-----------+\n",
    "|    P     |     0    |     1     |      2    |\n",
    "+----------+----------+-----------+-----------+\n",
    "|          |    0.33  |    0.33   |    0.33   |\n",
    "+----------+----------+-----------+-----------+\n",
    "\n",
    "P(H | P, C):\n",
    "+------+------+------+------+------+------+------+------+------+------+\n",
    "|   C  |          0         |          1         |          2         |\n",
    "+------+------+------+------+------+------+------+------+------+------+\n",
    "|   P  |   0  |   1  |   2  |   0  |   1  |   2  |   0  |   1  |   2  |\n",
    "+------+------+------+------+------+------+------+------+------+------+\n",
    "|  H=0 |   0  |   0  |   0  |   0  |  0.5 |   1  |   0  |   1  |  0.5 | \n",
    "+------+------+------+------+------+------+------+------+------+------+\n",
    "|  H=1 |  0.5 |   0  |   1  |   0  |   0  |   0  |   1  |   0  |  0.5 |\n",
    "+------+------+------+------+------+------+------+------+------+------+\n",
    "|  H=2 |  0.5 |   1  |   0  |   1  |  0.5 |   0  |   0  |   0  |   0  |\n",
    "+------+------+------+------+------+------+------+------+------+------+\n",
    "</pre>\n",
    "\n",
    "Let's say that the contestant selected door 0 and the host opened door 2, we need to find the probability of the prize i.e. $P(P|H=2, C=0)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Finding Elimination Order: : : 0it [00:19, ?it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<TabularCPD representing P(C:3) at 0x7f580a175310>,\n",
       " <TabularCPD representing P(P:3) at 0x7f58128ad520>,\n",
       " <TabularCPD representing P(H:3 | C:3, P:3) at 0x7f580a175340>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pgmpy.models import DiscreteBayesianNetwork\n",
    "from pgmpy.factors.discrete import TabularCPD\n",
    "\n",
    "# Defining the network structure\n",
    "model = DiscreteBayesianNetwork([(\"C\", \"H\"), (\"P\", \"H\")])\n",
    "\n",
    "# Defining the CPDs:\n",
    "cpd_c = TabularCPD(\"C\", 3, [[0.33], [0.33], [0.33]])\n",
    "cpd_p = TabularCPD(\"P\", 3, [[0.33], [0.33], [0.33]])\n",
    "cpd_h = TabularCPD(\n",
    "    \"H\",\n",
    "    3,\n",
    "    [\n",
    "        [0, 0, 0, 0, 0.5, 1, 0, 1, 0.5],\n",
    "        [0.5, 0, 1, 0, 0, 0, 1, 0, 0.5],\n",
    "        [0.5, 1, 0, 1, 0.5, 0, 0, 0, 0],\n",
    "    ],\n",
    "    evidence=[\"C\", \"P\"],\n",
    "    evidence_card=[3, 3],\n",
    ")\n",
    "\n",
    "# Associating the CPDs with the network structure.\n",
    "model.add_cpds(cpd_c, cpd_p, cpd_h)\n",
    "\n",
    "# Some other methods\n",
    "model.get_cpds()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check_model check for the model structure and the associated CPD and returns True if everything is correct otherwise throws an exception\n",
    "model.check_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Finding Elimination Order: : : 0it [00:00, ?it/s]\n",
      "0it [00:00, ?it/s]\u001b[A"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+------+----------+\n",
      "| P    |   phi(P) |\n",
      "+======+==========+\n",
      "| P(0) |   0.3333 |\n",
      "+------+----------+\n",
      "| P(1) |   0.6667 |\n",
      "+------+----------+\n",
      "| P(2) |   0.0000 |\n",
      "+------+----------+\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# Infering the posterior probability\n",
    "from pgmpy.inference import VariableElimination\n",
    "\n",
    "infer = VariableElimination(model)\n",
    "posterior_p = infer.query([\"P\"], evidence={\"C\": 0, \"H\": 2})\n",
    "print(posterior_p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We see that the posterior probability of having the prize behind door 1 is more that door 0. Therefore the contestant should switch the door."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
